package com.itheima.utils;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

public class RequestContextUtil {

    public static String getHeader(String headerName){
        //获取路由转发的头信息
        String headerValue = RequestContextUtil.getRequest().getHeader(headerName);
        return headerValue;
    }

    public static HttpServletRequest getRequest(){
        // RequestContextHolder: 请求对象的上下文持有者, 一次线程执行过程.都有请求对象的相关信息, 使用了ThreadLocal
        ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
        // 请求对象
        HttpServletRequest request = requestAttributes.getRequest();
        return request;
    }

}